import AbstractRuntimeView from "../../../common/abstract/mvc/AbstractRuntimeView";
import Event from "../../../common/manager/EventClick";
import { ZYEventMgr } from "../../../common/manager/YLEventMgr";
import MyTools from "../../../common/util/MyTools";
import { ADMgr } from "../../../common/wxComponent/ADManager";
import adModel from "./adModel";
/**
 * 
 * @author: xj.huang
 * @date: 2021/08/03 17:46:25
 */
export default class BannerView extends AbstractRuntimeView<null, null>  {
    private BannerNode: Laya.Image;
    private ban_list: Laya.List;
    constructor() {
        super()
    }
    onOnce() {
        // super.onOnce()
        this.onAddEvent()
        this.BannerNode.visible = false;
        this.onBannerMode(false, false)
        ADMgr.init()
        this.zOrder = 99;
        this.updataItem(this.gameConfig.game_box)
    }
    private onAddEvent() {
        ZYEventMgr.on(ADEvent.NOT_AD, this, this.onHideCellBanner)
        ZYEventMgr.on(ADEvent.SDK_BANNER_MODE, this, this.onBannerMode)
        ZYEventMgr.on(ADEvent.SHOW_BOTTOM_AD, this, this.onShowCellBanner)
        ZYEventMgr.on(ADEvent.HIDE_BOTTOM_AD, this, this.onHideCellBanner)
        this.ban_list.renderHandler = new Laya.Handler(this, this.onUpServerItem);
        this.ban_list.hScrollBarSkin = ""
    }
    private updataItem(data = []) {
        this.ban_list.array = MyTools.maopao(data);
        this.ban_list.refresh()
        // console.log("this.ban_list.numChildren",this.ban_list.getChildAt(0).numChildren)
    }
    /**更新服务器格子广告的数据 */
    private onUpServerItem(item: Laya.Box, index) {
        let gameicon = item.getChildByName("gameicon") as Laya.Image;
        let lblName = item.getChildByName("lblName") as Laya.Label;
        let hot = item.getChildByName("hot") as Laya.Image;
        gameicon.skin = item.dataSource.icon_url;
        gameicon['id'] = item.dataSource.id;
        hot.visible = item.dataSource.is_hot !== 0 ? true : false;
        lblName.text = item.dataSource.name;
        Event.offClick(item, this, this.onClickItem)
        Event.onClick(item, this, this.onClickItem)
    }

    private onClickItem(e: Laya.Event) {
        let dataSource = e.target['dataSource'];
        let exportData = null;
        for (let i = 0; i < this.gameConfig.game_box.length; i++) {
            if (dataSource.id == this.gameConfig.game_box[i].id) {
                exportData = this.gameConfig.game_box[i];
                break;
            }
        }
        if (exportData == null) {
            console.log("无挑战信息");
            return;
        }
        if (this.gameConfig.box_conf.force_ItemCount === 0 || !this.gameConfig.box_conf.force_ItemCount) {
            //原框架这里重新刷新list-itme数据,不太理解为什么这样写
            console.warn("无法跳转");
            return
        }
        ADMgr.openWxAPP(MyTools.strim(exportData.app_id), exportData.path,exportData)
        
    }

    /**显示格子广告 */
    private onShowCellBanner() {
        Laya.timer.clearAll(this)
        Laya.timer.once(3000,this,()=>{
            !adModel.bannerShowType&&(this.BannerNode.visible = Laya.Browser.onWeiXin );
        })
    }
    /**隐藏格子广告 */
    private onHideCellBanner() {
        Laya.timer.clearAll(this)
        this.BannerNode.visible = false;
        console.log("this.BannerNod",this.BannerNode)
    }
    onBannerMode(gird, banner) {
        // console.log("!gird&&!banner", gird, banner)
        if (!gird && !banner) {
            this.onShowCellBanner()
        } else {
            this.onHideCellBanner()
        }
    }
}